package com.kiln.juxingqs.ui.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import client.global.commlib.network.data.JsonResult
import client.global.commlib.network.data.Ret
import com.kiln.juxingqs.api.bean.GoodsOrder
import com.kiln.juxingqs.ui.viewmodel.MiddleVm


class WdjpViewModel(app: Application) : MiddleVm(app) {
    val isShowing = MutableLiveData<Boolean>(false)

    val isOneLoading = MutableLiveData<Boolean>(false)
    val isTwoLoading = MutableLiveData<Boolean>(false)
    val isThreeLoading = MutableLiveData<Boolean>(false)
    val pageOne = MutableLiveData<Int>(0)
    val pageTwo = MutableLiveData<Int>(0)
    val pageThree = MutableLiveData<Int>(0)

    val callOne = MutableLiveData<JsonResult<GoodsOrder>>()
    val callTwo = MutableLiveData<JsonResult<GoodsOrder>>()
    val callThree = MutableLiveData<JsonResult<GoodsOrder>>()
    val cancleRet = MutableLiveData<JsonResult<Ret>>()

    val goods_id = MutableLiveData<String>("")

    fun holdOne() {
        if (isOneLoading.value!!)
            return
        hold({
            server.buy_order_record_by_id(pageOne.value!!, 1, goods_id.value!!)
        }, {
            isOneLoading.value = true
            pageOne.value = pageOne.value!! + 1
        }, {
            callOne.value = it
        }, complete = {
            isOneLoading.value = false
        })
    }

    fun holdTwo() {
        if (isTwoLoading.value!!)
            return
        hold({
            server.buy_order_record_by_id(pageTwo.value!!, 2, goods_id.value!!)
        }, {
            isTwoLoading.value = true
            pageTwo.value = pageTwo.value!! + 1
        }, {
            callTwo.value = it
        }, complete = {
            isTwoLoading.value = false
        })
    }

    fun holdThree() {
        if (isThreeLoading.value!!)
            return
        hold({
            server.buy_order_record_by_id(pageThree.value!!, 0, goods_id.value!!)
        }, {
            isThreeLoading.value = true
            pageThree.value = pageThree.value!! + 1
        }, {
            callThree.value = it
        }, complete = {
            isThreeLoading.value = false
        })
    }

    fun cancle(b: GoodsOrder.Order) {
        hold({
            server.del_buy_order("${b.order_id}")
        }, {
            isShowing.value = true
        }, {
            cancleRet.value = it
        }, complete = {
            isShowing.value = false
        })
    }
}